/*
 * Vector.cpp
 *
 *  Created on: 2013.01.10.
 *      Author: Thg
 */

#include "Vector.h"


Vector::Vector()
{
	mX = mY = mZ = 0.0f;
}

Vector::Vector(float x, float y, float z)
{
	this->mX = x;
	this->mY = y;
	this->mZ = z;
}

Vector
Vector::operator+(const Vector& v)
{
	return Vector(mX + v.mX, mY + v.mY, mZ + v.mZ);
}

Vector
Vector::operator-(const Vector& v)
{
	return Vector(mX - v.mX, mY - v.mY, mZ - v.mZ);
}

Vector
Vector::operator*(float f)
{
	return Vector(mX * f, mY * f, mZ * f);
}

float
Vector::operator*(const Vector& v)
{
	return (mX * v.mX + mY * v.mY + mZ * v.mZ);
}

Vector
Vector::operator%(const Vector& v)
{
	return Vector(mY * v.mZ - mZ * v.mY, mZ * v.mX - mX * v.mZ, mX * v.mY - mY * v.mX);
}

float
Vector::length()
{
	return (float)sqrt(mX * mX + mY * mY + mZ * mZ);
}

void
Vector::operator+=(const Vector& v)
{
	mX += v.mX;
	mY += v.mY;
	mZ += v.mZ;
}

void
Vector::operator-=(const Vector& v)
{
	mX -= v.mX;
	mY -= v.mY;
	mZ -= v.mZ;
}

void
Vector::operator*=(float f)
{
	mX *= f;
	mY *= f;
	mZ *= f;
}

Vector
Vector::operator/(float f)
{
	return Vector(mX/f, mY/f, mZ/f);
}

void
Vector::normalize()
{
	float l = length();
	if (l < 0.000001f)
	{
		mX = 1;
		mY = 0;
		mZ = 0;
	}
	else
	{
		mX /= l;
		mY /= l;
		mZ /= l;
	}
}

Vector
Vector::getUnitVector() const
{
	Vector r = *this;
	r.normalize();
	return r;
}

Vector
Vector::rotate(const Vector& axis, float angle)
{
	Vector uv = this->getUnitVector();
	Vector uvMul = axis.getUnitVector() % this->getUnitVector();
	float radian = angle * M_PI / 180;
	return (uv * cos(radian) +  uvMul * sin(radian));
}

float*
Vector::getArray()
{
	return &mX;
}

float
Vector::x() const
{
	return mX;
}

float
Vector::y() const
{
	return mY;
}

float
Vector::z() const
{
	return mZ;
}

void
Vector::x(float x)
{
	mX = x;
}
void
Vector::y(float y)
{
	mY = y;
}
void
Vector::z(float z)
{
	mZ = z;
}
